package cn.lyq.card.base

import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.annotation.LayoutRes
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import cn.lyq.card.R
import com.blankj.utilcode.util.BusUtils
import com.blankj.utilcode.util.ScreenUtils


abstract class BaseDialogFg : DialogFragment() {

    @get:LayoutRes
    abstract val rootId: Int

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        var rootView: View? = null
        dialog?.window?.let { window ->
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            }
            //需要用android.R.id.content这个view
            val viewGroup = window.findViewById<View>(R.id.content) as ViewGroup?
            rootView = inflater.inflate(rootId, viewGroup, false)
            window.setBackgroundDrawable(ColorDrawable(Color.parseColor("#66000000")))
            val dialogHeight = ScreenUtils.getScreenHeight()
            //设置弹窗大小为会屏
            window.setLayout(
                ViewGroup.LayoutParams.MATCH_PARENT,
                if (dialogHeight == 0) ViewGroup.LayoutParams.MATCH_PARENT else dialogHeight
            )
        }
        if (rootView == null) {
            rootView = inflater.inflate(rootId, null, false)
        }

        BusUtils.register(this)
        return rootView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initVM()
        initData()
        initView(view)
        initListener(view)
        initObserver()
    }

    open fun initVM() {}

    open fun initData() {}

    open fun initView(view: View) {}

    open fun initListener(view: View) {}

    open fun initObserver() {}

    fun <T : ViewModel> getVM(
        vm: Class<T>,
        factory: ViewModelProvider.Factory = ViewModelProvider.NewInstanceFactory()
    ) = ViewModelProvider(this, factory).get(vm)

    fun <T : ViewModel> getVMActivity(
        vm: Class<T>,
        factory: ViewModelProvider.Factory = ViewModelProvider.NewInstanceFactory()
    ) = ViewModelProvider(requireActivity(), factory).get(vm)

    override fun onStart() {
        super.onStart()
        dialog?.window?.run {
            val param = attributes
            param.dimAmount = 0f
            attributes = param
        }
    }

    fun startActivity(clazz: Class<*>) {
        startActivity(Intent(requireContext(), clazz))
    }

    /**
     * Can not perform this action after onSaveInstanceState
     */
    override fun dismiss() {
        try {
            super.dismiss()
        } catch (e: Exception) {
            dismissAllowingStateLoss()
        }
    }

    fun show(manager: FragmentManager) {
        show(manager, javaClass.simpleName)
    }

    fun show(activity: BaseActivity) {
        show(activity.supportFragmentManager, javaClass.simpleName)
    }

    fun showLoading(){}

    fun dismissLoading(){}

    /**
     * Can not perform this action after onSaveInstanceState
     */
    override fun show(manager: FragmentManager, tag: String?) {
        try {
            super.show(manager, tag)
        } catch (e: Exception) {
            val ft = manager.beginTransaction()
            ft.add(this, tag)
            ft.commitAllowingStateLoss()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        BusUtils.unregister(this)
    }

}